package net.butfly.bus.filter;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import net.butfly.albacore.exception.SystemException;
import net.butfly.bus.Constants;
import net.butfly.bus.Request;
import net.butfly.bus.Response;

public class ValidateFilter extends FilterBase implements Filter {
	private Validator validator;

	@Override
	public void execute(Request request, Response response) throws Exception {
		if (request.arguments() == null || request.arguments().length == 0)
			return;
		Set<ConstraintViolation<Object>> violations;
		StringBuilder validateErrMsg = null;
		for (Object dto : request.arguments()) {
			violations = validator.validate(dto);
			Iterator<ConstraintViolation<Object>> it = violations.iterator();
			while (it.hasNext()) {
				validateErrMsg = validateErrMsg != null ? validateErrMsg
						: new StringBuilder();
				ConstraintViolation<Object> cv = it.next();
				validateErrMsg.append("[参数对象:" + cv.getRootBeanClass() + ",");
				validateErrMsg.append("属性:" + cv.getPropertyPath() + ",");
				validateErrMsg.append("错误信息:" + cv.getMessage() + "]");
			}
		}
		if (validateErrMsg != null)
			throw new SystemException(Constants.UserError.BAD_REQUEST,
					validateErrMsg.toString());
		super.execute(request, response);
	}

	@Override
	public void initialize(Map<String, String> params) {
		super.initialize(params);
		this.validator = Validation.buildDefaultValidatorFactory()
				.getValidator();
	}
}
